<template>
  <div class="app-container">
    <!-- 查询模块 -->
    <div class="formDiv">
      <el-form ref="form" :inline="true" label-width="140px">
        <el-row>
          <el-col :span="6">
            <el-form-item label="姓名">
              <el-input v-model="form.realname" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="手机号">
              <el-input v-model="form.username" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="级别">
              <el-select v-model="form.roleId" placeholder="请选择">
                <el-option
                  v-for="item in roleOptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="交钱分公司">
              <el-select v-model="form.payBranchId" placeholder="请选择">
                <el-option
                  v-for="item in officeOption"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="账号状态">
              <el-select v-model="form.registerStatus" placeholder="请选择">
                <el-option
                  v-for="item in options"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="加入时间">
              <el-date-picker
                v-model="dates"
                value-format="yyyy-MM-dd HH:mm:ss"
                type="daterange"
                range-separator="-"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                :default-time="['00:00:00', '23:59:59']"
              >></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="账号类型">
              <el-select v-model="form.type" placeholder="请选择">
                <el-option
                  v-for="item in typeOption"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="推荐人手机号">
              <el-input v-model="form.refereePhone"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item>
              <el-button style="margin-left:60px" type="primary" @click="search">查询</el-button>
              <el-button
                style="margin-left:30px"
                type="primary"
                @click="exfun"
                v-if="$store.state.user.permissionJson.indexOf('AdminUserServerProvider.exportMmkUserServerList') !=-1"
              >导出</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <!-- 主页列表 -->
    <div v-loading="tableLoading" class="listDiv">
      <!-- table列表 -->
      <el-table :data="tableData" class="tableData" style="width: 100%">
        <el-table-column prop="realname" label="姓名" />
        <el-table-column prop="username" label="手机号">
          <template slot-scope="scope">
            <span>{{scope.row.username | phonereg}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="roleName" label="级别" />
        <el-table-column label="证件类型">
          <template slot-scope="scope">
            <span>{{ scope.row.cardType | cardNull }}</span>
          </template>
        </el-table-column>
        <el-table-column label="证件号码">
          <template slot-scope="scope">
            <span v-if="scope.row.card == 'null'" />
            <span v-else>{{ scope.row.card }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="areaName" label="地区" />
        <el-table-column prop="areaName" label="推荐人">
          <template slot-scope="scope">
            {{scope.row.refereeName}}
            <br />
            {{scope.row.refereePhone| phonereg}}
          </template>
        </el-table-column>
        <el-table-column prop="team" label="团队" />
        <el-table-column label="签约类型">
          <template slot-scope="scope">
            <span v-if="scope.row.signType" v-html="scope.row.signType == 1 ? '赠送' : '购买'" />
          </template>
        </el-table-column>
        <el-table-column prop="signTime" label="加入时间" />
        <el-table-column prop="branchName" label="交钱分公司" />
        <el-table-column prop="money" label="加盟金额" />
        <el-table-column label="账号类型">
          <template slot-scope="scope">
            <span>{{ scope.row.type | accountType }}</span>
          </template>
        </el-table-column>
        <!-- <el-table-column
                    label="账号状态"
                >
                    <template slot-scope="scope">
                        <el-switch
                        class="switchStyle"
                        v-model="scope.row.status"
                        @click.native="changeStatus(scope.row.userId,'status',scope.row.status)"
                        active-color="#13ce66"
                        inactive-color="#eee"
                        :active-value="0"
                        :inactive-value="1">
                        </el-switch>
                    </template>
        </el-table-column>-->
        <el-table-column prop="superCode" label="推荐码" />
        <el-table-column label="账号状态">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.accountStatus"
              disabled
              class="switchStyle switchStyleOpa"
              active-color="#13ce66"
              inactive-color="#eee"
              :active-value="0"
              :inactive-value="1"
              @click.native="
                clickStatus(
                  scope.row.userId,
                  scope.row.accountStatus,
                  'accountStatus'
                )
              "
            />
          </template>
        </el-table-column>
        <el-table-column align="center" label="提现冻结">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.withdrawStatus"
              class="switchStyle switchStyleOpa"
              disabled
              active-color="#13ce66"
              inactive-color="#eee"
              :active-value="0"
              :inactive-value="1"
              @click.native="
                clickStatus(
                  scope.row.userId,
                  scope.row.withdrawStatus,
                  'withdrawStatus'
                )
              "
            />
          </template>
        </el-table-column>
        <el-table-column align="center" label="积分冻结">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.integralStatus"
              class="switchStyle switchStyleOpa"
              disabled
              active-color="#13ce66"
              inactive-color="#eee"
              :active-value="0"
              :inactive-value="1"
              @click.native="
                clickStatus(
                  scope.row.userId,
                  scope.row.integralStatus,
                  'integralStatus'
                )
              "
            />
          </template>
        </el-table-column>
        <el-table-column align="center" width="120" label="备注">
          <template slot-scope="scope">
            <div>
              <p>账号：{{ scope.row.accountStatusRemark }}</p>
              <p>提现：{{ scope.row.withdrawStatusRemark }}</p>
              <p>积分：{{ scope.row.integralStatusRemark }}</p>
            </div>
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="100">
          <template slot-scope="scope">
            <el-button
              v-if="
                hasPermission('AdminUserServerProvider.modifyUserServerInfo')
              "
              type="text"
              size="small"
              @click="toRoutes('designerManageEdit', scope.row)"
            >编辑</el-button>
            <el-button
              v-if="
                hasPermission('AdminUserServerProvider.getMmkUserServerDetail')
              "
              type="text"
              size="small"
              @click="toRoutes('designerManageInfo', scope.row)"
            >详情</el-button>
            <div
              v-if="
                hasPermission(
                  'AdminUserRecommendProvider.updateTeamRelationship'
                )
              "
            >
              <el-button
                v-if="scope.row.roleId == '7' || scope.row.roleId == '8'"
                type="text"
                size="small"
                @click="toRoutes('designerInfoTeam', scope.row)"
              >是否团队</el-button>
            </div>
            <div>
              <el-button
                v-if="
                  scope.row.roleName == '美学设计师' && scope.row.type == '4'
                "
                type="text"
                size="small"
                @click="releaseFn()"
              >释放</el-button>
            </div>
            <el-button
              v-if="
                hasPermission('AdminUserServerProvider.modifyUserServerRole')
              "
              type="text"
              size="small"
              @click="toRoutes('adjustLevel', scope.row)"
            >调整级别</el-button>
            <el-button
              type="text"
              v-if="
                scope.row.accountType == 8 ||
                  (scope.row.completionPayment == 1 &&
                    scope.row.accountType == 1)
              "
              size="small"
              @click="openfn(scope.row)"
            >
              {{
              scope.row.accountType == 1 ? "补齐尾款信息" : "补齐尾款"
              }}
            </el-button>
            <el-button
              type="text"
              size="small"
              v-if="hasPermission('AdminUserProvider.replacePhone')"
              @click="changePhone(scope.row)"
            >更换手机号</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 冻结解冻原因 -->
      <el-dialog
        :title="dialogsTitle"
        :visible.sync="dialogs"
        :before-close="handleClose"
        width="468px"
      >
        <el-form ref="freezeForm" :inline="true" label-width="140px">
          <el-form-item :label="dialogsTitle">
            <el-input v-model="freezeForm.freeze" />
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="handleClose()">取 消</el-button>
          <el-button type="primary" @click="submitBtns()">确 定</el-button>
        </span>
      </el-dialog>
      <!-- 分页 -->
      <div class="pagesCon">
        <div>
          <el-pagination
            :current-page="form.pageNum"
            :page-sizes="page_sizes"
            :page-size="form.limit"
            layout="total, sizes, prev, pager, next, jumper"
            :total="page_totals"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </div>
    </div>

    <el-dialog
      :title="addparams.completionPayment == 1 ? '补齐尾款信息' : '补齐尾款'"
      :visible.sync="dialogVisible"
      width="30%"
    >
      <el-form ref="freezeForm" :inline="true" label-width="140px">
        <el-form-item label="补交手机号" v-if="addparams.completionPayment != 1">{{ addparams.username }}</el-form-item>
        <el-form-item label="补交金额">
          <el-input v-model="addparams.tailAmount" :disabled="addparams.completionPayment == 1" />
        </el-form-item>
        <el-form-item label="补交方式">
          <el-select
            v-model="addparams.tailType"
            :disabled="addparams.completionPayment == 1"
            placeholder="请选择"
          >
            <el-option label="现金支付" :value="1"></el-option>
            <el-option label="招商业绩抵扣" :value="2"></el-option>
            <el-option label="业绩达标" :value="3"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="补交时间" v-if="addparams.completionPayment == 1">
          <el-input v-model="addparams.tailTime" :disabled="addparams.completionPayment == 1" />
        </el-form-item>
        <el-form-item label="备注">
          <el-input
            type="textarea"
            :rows="2"
            :disabled="addparams.completionPayment == 1"
            v-model="addparams.tailRemark"
          />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" v-if="addparams.completionPayment != 1" @click="surefn">确 定</el-button>
      </span>
    </el-dialog>
    <!-- //更换手机号 -->
    <el-dialog title="更换手机号" :visible.sync="phonedialogVisible" width="30%">
      <el-form :inline="true" label-width="100px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="原手机号">{{params.username}}</el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="变更手机号">
              <el-input v-model="params.phone"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="phonedialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="sure">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import {
  getList,
  modifyUserServerStatus,
  changeAccountStatus,
  changeWithdrawStatus,
  finishPay,
  changeIntegralStatus
} from "@/api/designerManage";
import { replacePhone } from "@/api/vipIndex";
import { exportfile } from "@/api/commjs/exprot";
import { companyList } from "@/api/branchOfficeDropDown";
export default {
  name: "DesignerManageIndex",
  filters: {
    cardNull(e) {
      return window.helper.cardType(String(e));
    },
    accountType(e) {
      return window.helper.accountType(String(e));
    }
  },
  data() {
    return {
      addparams: {},
      apis: null,
      dialogsTitle: null,
      dialogs: false,
      freezeForm: {
        freeze: null
      },
      officeOption: [],
      tableLoading: true,
      dates: [],
      value1: "",
      options: [
        {
          value: "0",
          label: "启用"
        },
        {
          value: "1",
          label: "停用"
        }
      ],
      typeOption: [],
      form: {
        pageNum: 1,
        pageSize: 10,
        realname: null,
        roleId: null,
        payBranchId: null,
        signTimeStart: null,
        signTimeEnd: null,
        registerStatus: null,
        username: null
      },
      params: {},
      roleOptions: [
        // {
        //   value: "1",
        //   label: "普通用户"
        // },
        {
          value: "2",
          label: "特约授权经销商"
        },
        {
          value: "3",
          label: "高级授权经销商"
        },
        {
          value: "7",
          label: "普通合伙人授权经销商"
        },
        {
          value: "8",
          label: "高级合伙人授权经销商"
        }
      ],
      tableData: [],
      dialogVisible: false,
      phonedialogVisible: false,
      page_sizes: [10, 30, 50, 100, 1000],
      page_totals: 60,
      listForm: {
        pageNum: 1,
        pageSize: 10
      }
    };
  },
  inject: ["reload"],
  mounted() {
    this.getList(this.form);
    this.getDrop();
    this.typeOption = window.helper.accountType("listReturn");
    // 获取级别
    // this.roleOptions = window.helper.roleFn("listReturn");
  },
  methods: {
    //导出
    exfun() {
      exportfile(
        "/com.mmk.member.api.AdminUserServerProvider/1.0.0/exportMmkUserServerList.html",
        this.form
      );
    },
    //surefn
    async surefn() {
      if (
        this.addparams.tailAmount == 0 ||
        !this.addparams.tailType ||
        !this.addparams.tailRemark
      ) {
        this.$message.error("所有信息不能为空");
        return;
      }
      let res = await finishPay(this.addparams);
      console.log(res);
      if (res.data.code === 0) {
        this.$message({
          message: "修改成功",
          type: "success"
        });
        this.getList(this.form);
      } else {
        this.$message.error(res.data.msg);
      }
      if (res.data.code === 0) this.dialogVisible = false;
    },
    //openfn
    openfn(datas) {
      this.addparams = datas;
      this.dialogVisible = true;
    },
    // 按钮权限
    hasPermission(e) {
      return window.helper.hasPermission(e);
    },
    // 上下架与状态修改
    changeStatus(userId, type, status) {
      const datas = {
        userId: userId
      };
      datas[type] = status;
      modifyUserServerStatus(datas).then(res => {
        // console.log(res)
        if (res.data.code === 0) {
          this.$message({
            message: "修改成功",
            type: "success"
          });
        } else {
          this.$message.error("修改失败");
        }
      });
      console.log(datas);
    },
    // 美学设计师管理->修改账户状态
    changeAccountStatus(datas) {
      changeAccountStatus(datas)
        .then(res => {
          if (!res.data.code) {
            this.succStatus("修改");
            this.getList(this.form);
          } else {
            this.$message.error(res.data.msg);
          }
          this.reload();
        })
        .catch(error => {
          this.$message.error(error);
          this.reload();
        });
    },
    // 美学设计师管理->修改提现状态
    changeWithdrawStatus(datas) {
      changeWithdrawStatus(datas)
        .then(res => {
          if (!res.data.code) {
            this.succStatus("修改");
            this.getList(this.form);
          } else {
            this.$message.error(res.data.msg);
          }
          this.reload();
        })
        .catch(error => {
          this.$message.error(error);
          this.reload();
        });
    },
    // 美学设计师管理->修改积分状态
    changeIntegralStatus(datas) {
      changeIntegralStatus(datas)
        .then(res => {
          if (!res.data.code) {
            this.succStatus("修改");
            this.getList(this.form);
          } else {
            this.$message.error(res.data.msg);
          }
          this.reload();
        })
        .catch(error => {
          this.$message.error(error);
          this.reload();
        });
    },
    //修改手机号
    changePhone(datas) {
      console.log(datas);
      this.params = datas;
      this.params["type"] = 10;
      this.phonedialogVisible = true;
    },
    async sure() {
      console.log(this.params);
      let res = await replacePhone(this.params);
      if (res.data.code == 0) {
        this.$message.success(res.data.msg);
        this.phonedialogVisible = false;
        this.getList(this.form);
      } else {
        this.$message.warning(res.data.msg);
      }
    },
    // 获取相关列表
    getDrop() {
      companyList()
        .then(res => {
          console.log(res);
          const resData = res.data.data;
          const newData = [];
          resData.forEach(e => {
            newData.push({
              value: e.id,
              label: e.name
            });
          });
          this.officeOption = newData;
        })
        .catch(error => {
          this.$message.error(error);
        });
    },
    // 成功状态弹窗
    succStatus(txt) {
      this.$message({
        message: txt + "成功",
        type: "success"
      });
    },
    // 失败状态弹窗
    errorStatus(txt) {
      this.$message.error(txt + "失败");
    },
    // 查询
    search() {
      this.form.pageNum = 1;
      if (this.dates) {
        this.form.signTimeStart = this.dates[0];
        this.form.signTimeEnd = this.dates[1];
      } else {
        this.form.signTimeStart = null;
        this.form.signTimeEnd = null;
      }
      // console.log(this.form)
      this.getList(this.form);
    },
    // 路由跳转
    toRoutes(e, row) {
      const rows = JSON.stringify(row);
      // console.log(ac)
      const route = { name: e, query: { userId: rows } };
      this.$router.push(route);
    },
    // 列表数据获取
    getList(e) {
      this.tableLoading = true;
      const datas = e;
      getList(datas)
        .then(res => {
          if (!res.data.code) {
            // console.log(res)
            this.tableData = res.data.data.list;
            this.page_totals = res.data.data.count;
          } else {
            this.$message.error(res.data.msg);
          }
          this.tableLoading = false;
        })
        .catch(error => {
          this.$message.error(error);
        });
    },
    // 删除
    deleteFn(e) {
      const datas = {
        id: e
      };
      this.$confirm("删除该条数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          deleteData(datas)
            .then(res => {
              console.log(res);
              if (!res.data.code) {
                this.succStatus("删除");
                this.getList(this.form);
              } else {
                this.$message.error(res.data.msg);
              }
            })
            .catch(error => {
              this.$message.error(error);
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    // 释放
    releaseFn(e) {
      // let datas = {
      //     id:e
      // }
      this.$confirm("是否确认将试用美学设计师角色变更为普通用户?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          // deleteData(datas).then(res =>{
          //     console.log(res)
          //     if(!res.data.code){
          //         this.succStatus("删除")
          //         this.getList(this.listForm)
          //     }else{
          //         this.$message.error(res.data.msg);
          //     }
          // })
          // .catch(error =>{
          //     this.$message.error(error);
          // })
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消更改"
          });
        });
    },
    // 改变页面条数
    handleSizeChange(val) {
      this.form.pageSize = val;
      this.getList(this.form);
    },
    // 选择页数
    handleCurrentChange(val) {
      this.form.pageNum = val;
      this.getList(this.form);
    },
    // switch开关
    clickStatus(userId, status, apis) {
      // console.log(userId,status,apis)
      this.apis = apis;
      this.dialogs = true;
      this.freezeForm["userId"] = userId;
      this.freezeFormStatus = status;
      if (status == 1) {
        this.dialogsTitle = "解冻原因";
      } else {
        this.dialogsTitle = "冻结原因";
      }
    },
    // 弹窗提交
    submitBtns() {
      if (this.freezeForm.freeze) {
        if (this.freezeFormStatus == 0) {
          this.freezeFormStatus = 1;
        } else {
          this.freezeFormStatus = 0;
        }
        if (this.apis === "accountStatus") {
          this.freezeForm["accountStatus"] = this.freezeFormStatus;
          this.freezeForm["accountStatusRemark"] = this.freezeForm.freeze;
          delete this.freezeForm.freeze;
          this.changeAccountStatus(this.freezeForm);
        }
        if (this.apis === "withdrawStatus") {
          this.freezeForm["withdrawStatus"] = this.freezeFormStatus;
          this.freezeForm["withdrawStatusRemark"] = this.freezeForm.freeze;
          delete this.freezeForm.freeze;
          this.changeWithdrawStatus(this.freezeForm);
        }
        if (this.apis === "integralStatus") {
          this.freezeForm["integralStatus"] = this.freezeFormStatus;
          this.freezeForm["integralStatusRemark"] = this.freezeForm.freeze;
          delete this.freezeForm.freeze;
          this.changeIntegralStatus(this.freezeForm);
        }
        this.dialogs = false;
        this.freezeForm = { freeze: null };
      } else {
        if (this.dialogsTitle == "解冻原因") {
          this.$message.error("请输入解冻原因，解冻原因为必填项");
        } else {
          this.$message.error("请输入冻结原因，冻结原因为必填项");
        }
      }
    },
    // 关闭冻结解冻弹窗
    handleClose() {
      this.dialogs = false;
    }
  }
};
</script>
<style scoped>
/* 分页 */
.pagesCon {
  height: 40px;
  margin-top: 20px;
}
.pagesCon > div {
  float: right;
}
/* 表单样式修改 */
.el-range-editor--medium.el-input__inner {
  width: 236px;
}
</style>
